set more off


qui {

use full_data, clear

*merge with CPI 
sort year
merge year using "${original}CPI_v13"
drop if _m==2

gen t=((year-1992)/2)

************************************************************
*Generate our cognitive variables
************************************************************
gen tot_recall=dlrc+imrc if !(fimrc==1|fdlrc==1)
gen miss_rec=(tot_recall==. |fpstme==1)


xtset hhidpn t
gen dtot_rec=d.tot_recall if miss_rec==0
gen d2tot_rec=d2.tot_recall if miss_rec==0

*generate quintiles and past values
gen ltot_r=l.tot_recall 
xtile qrec=tot_rec, nq(5)
xtile q4rec=tot_rec, nq(4)
gen lqrec=L.qrec 


*Generate absolute loss variables

gen loss_t=dtot_rec<-2 
replace loss_t=. if dtot_rec==.



*Generate the relative loss definition used in the paper based on 20% or more
xtset hhidpn t
gen rel_memc=(tot_recall-L.tot_recall)/L.tot_recall 

gen r_loss=rel_memc<-.21
replace r_loss = . if rel_memc==.

************************************************************
*Used only for old robustness checks
*absolute mild loss
gen mloss_t=dtot_rec<-1
replace mloss_t=. if dtot_rec==.

*absolute very severe loss
gen sloss_t=dtot_rec<-3
replace sloss_t=. if dtot_rec==.

*Alternative definitions used only for old robustness checks
gen r1_loss=rel_memc<-.16
replace r1_loss = . if rel_memc==.
gen r2_loss=rel_memc<-.26
replace r2_loss = . if rel_memc==.
************************************************************



*Memory self-assessments 

*cross-sectional used in Figure 2

gen vgood=slfmem<3
replace vgood = . if slfmem == .

*Self-rated memory invert the scale
gen srmem=(6-slfmem)


*longitudinal self-assesment used in the main text
gen stablemem=pstmem<3
replace stablemem = . if pstmem == .
replace stablem=. if fpstme==1 



*generate  (un)awareness groups

gen r_loss_u=r_loss==1 &  stablem==1
replace r_loss_u = . if r_loss == .

gen r_loss_a=r_loss==1 &  stablem==0
replace r_loss_a = . if r_loss==.

*self-rated decline with no loss
gen decline_nloss=stablem==0 & loss_t==0
replace decline_nloss = . if loss_t == .

*(un)awareness absolute definition
gen loss_a=loss_t==1 &  stablem==0
replace loss_a = . if loss_t==.
gen loss_u=loss_t==1 &  stablem==1
replace loss_u = . if loss_t == .

gen r_decline_nloss=stablem==0 & r_loss==0
replace r_decline_nloss = . if r_loss == .





/* Robustness for referee with alternative measures
forvalue n=1/2 {

gen r`n'_loss_a=r`n'_loss==1 &  stablem==0
replace r`n'_loss_a = . if r`n'_loss==.

gen r`n'_loss_u=r`n'_loss==1 &  stablem==1
replace r`n'_loss_u = . if r`n'_loss == .

gen r`n'_decline_nloss=stablem==0 & r`n'_loss==0
replace r`n'_decline_nloss = . if r`n'_loss == .
}
*/



label var r_loss "relative loss(20%)"
label var r_loss_u "relative loss(20%) unaware"
label var r_loss_a "relative loss(20%) aware"
label var r_decline_nloss "declare memory decline but no relative loss(20%)"


label var loss_t "absolute loss(3 words+)"
label var loss_u "absolute loss(3 words+) unaware"
label var loss_a "absolute loss(3 words+)aware"
label var decline_nloss "declare memory decline but no absolute loss (3 words+)"


*lag value of decline and memory
*Used in a robustness check to show that awareness is not due to previous losses
xtset hhidpn t
gen ldecline=l.r_decline
gen lrloss=l.r_loss
gen l2rloss=l2.r_loss
gen lc2rloss=lrloss+l2rloss

gen lloss_u=l.r_loss_u
gen lloss_a=l.r_loss_a
gen ldtot=l.dtot_rec
gen l2dtot=l.d2tot_rec

gen stable_loss=stablemem*loss_t



*other cognitive variables

replace ser7=. if fser7!=0
replace  vocab=. if fvoc!=0
replace  bwc20=. if fbwc20!=0
replace  mstot=. if fser7!=0 // !& fbwc20!=0


egen numeracy=rowtotal(num1 num2 num3)
recode numeracy(0=.) if num1==.

gen lnum=L.numeracy
replace lnum=L2.numeracy if lnum==.
replace lnum=L3.numeracy if lnum==.

xtset hhidpn  t

*past numeracy values. It starts in 2002.

gen num0=L.numeracy if year>2002
replace num0=L.num0 if num0==. & year>2002
forvalue n=2/6 {
replace num0=L`n'.num0 if num0==. & year>2002
}

egen st_mstot=std(mstot)  
egen st_ser7=std(ser7)
egen st_bwc20=std(bwc20)
egen st_voc=std(vocab)

label var st_mstot "standardized total mental status"
label var st_ser7 "standardized serial 7"
label var st_bwc20 "standardized backward counting"
label var st_voc "standardized fluency"


************************************************************
*robustness only for referees. Create loss variables for other tests
gen ser7_loss=D.ser7<=-1 if D.st_ser7!=. 
gen mstot_loss=D.mstot<=-1 if D.st_ms!=.

gen ser7_rel=(ser7-L.ser7)/L.ser7
gen mstot_rel=(mstot-L.mstot)/L.mstot
gen voc_rel=(voc-L.voc)/L.voc

gen dbw=(bwc20-L.bwc20) // not useful only 4% with a loss


gen ser_loss_r=ser7_rel<=-.25
replace ser_loss_r = . if ser7==.
gen ser7_loss_2r=ser7_rel<=-.2
gen bwc_loss=bwc20<2 & L.bwc20==2
gen voc_loss_r=voc_rel<=-.25
replace voc_loss_r = . if vocab==.
gen mstotloss_r=mstot_rel<=-.2
replace mstotloss_r = . if mstot>15
************************************************************
************************************************************




************************************************************
*Covariates and other variables used (recoding and transformation)
************************************************************


*age (centered on 60) and time variable
gen age= (agem_b/12)-60
gen ager=round(age+60)

gen female=ragender==2


*centering education on HS
gen educy= raedyrs-12

gen adlap=adla>0
gen iadlap=adlap>0
recode adlap  (1=.) if adla==.
recode iadlap  (1=.) if iadla==.
gen age2=age^2
gen work=lbrf<3
replace work=. if lbrf==.
gen alone=mstat==1 |mstat==3
replace alone=. if mstat==.
gen single=mstat>3
gen hs=educy>=0 & educy<3
gen college=educy>2
gen white=raracem==1
gen black=raracem==2
gen other=raracem==3




	
*health variables
gen srh=5-shlt
gen srhc=D.srh
gen adlapc=D.adlap
gen iadlapc=D.iadlap

gen adlac=D.adla
gen iadlac=D.iadla

gen h_shock=stroks+cancrs+hearts
gen serious_hc=stroke+cancre+hearte+diabe
gen serious2_hc=stroke+cancre+hearte

recode drinkd (.=0) if drink==0
recode drinkn (.=0) if drink==0
replace mammog=. if female==0
replace prost=. if female==1


*lagged values
gen llbrf=L.lbrf
gen lwork=L.work
gen lalone=L.alone
gen lfinr=L.finr
replace lfinr=F.finr if lfinr==.
gen ladlap=L.adlap
gen liadlap=L.iadlap
gen ladla=L.adla
gen liadla=L.iadla
gen lsrh=l.srh
gen lgood=lsrh>2
replace lgood=. if lsrh==.

*transfers
gen ch_transfer= chtr_amount
replace ch_transfer=0 if chtr==1
gen ch_amount=chtr_am
gen transfer= chtr==3
replace ch_amount=0 if transfer==0

xtile  q_ch_t=chtr_amount, nq(5)
replace q_ch_t=1 if chtr==1

*attrition indicators used for robutness checks
sort hhidpn t
bysort hhidpn: gen Out=loss_t[_n+1]==.
sort hhidpn t
bysort hhidpn: gen T=_N
sort hhidpn t
bysort hhidpn: gen W=_n
egen cohort=group(hacohort)


*difficulties managing money used for robustness checks
gen diffm=money>0
 replace diffm=. if money>9

gen h_ins=henum>0 if henum!=.
 
*respondents and partner death  data: robustness for referees
	format s_ddate %td
	format raddate %td
	format iwbeg %td
	format iwend %td
 
	gen s_year_d=year(s_ddate)
	gen s_month_d=month(s_ddate)

	gen r_year_d=year(raddate)
	gen r_month_d=month(raddate)

	bysort hhidpn: egen p_ddate=max(s_ddate)
	bysort hhidpn: egen r_ddate=max(raddate)

	gen d_pdeath=iwend-p_ddate if 	p_ddate<iwend
	gen d_int=iwend-L.iwend
	gen p_death_new=d_pdeath<d_int & d_int!=.

	gen d_rdeath=r_ddate-iwend if r_ddate!=. & iwend!=.

	gen d_r2years=d_rdeath<730 &d_rdeath>=0 
	gen d_r5years=d_rdeath<1825  &d_rdeath>=0 

	gen new_single=single==1 &L.single==0
	replace new_single=0 if F.single==0
************************************************************************************************************************
************************************************************************************************************************
 
 
*************************************************************
*Create FIRST DIFFERENCES of wealth related variables*********
***********************************************************


*definitions
* HwATOTF = Sum (HwASTCK, HwACHCK, HwACD, HwABOND, HwAOTHR) - HwADEBT
* HwATOTN = Sum (HwARLES, HwATRAN, HwABSNS, HwAIRA, HwASTCK, HwACHCK, HwACD, HwABOND, HwAOTHR) - HwADEBT
* = Sum (HwARLES, HwATRAN, HwABSNS, HwAIRA)
* ARLES : Assets:Other Real estate
* ATRAN : Assets:Transportation
* ABSNS : Assets:Business
* AIRA : Assets:IRA (Individual Retirement Account)




*drop HRS variable because there are imputations when a value is missing among two non-consecutive waves
drop h_atotbc h_atotfc h_atotnc




** transform to real value 2014 using the CPI
* wealth and consumption levels
foreach x of varlist evf_totf evf_chck  evf_debt evf_stck evf_cd evf_bond h_atotf h_atotn  h_arles h_absns h_atotb  h_aothr atotf h_astck h_acd h_achck h_abond h_adebt h_atran dtotb h_aira h_atoth h_ahoub oopmd chtr_amount cams_ctots cams_cmort cams_ctranss cams_cndur cams_cdurs  cams_cautoall cams_chouss  {
	
replace `x'=`x'*236.7/CPI/1000


*Create lag and log values of listed variables
g l`x'=L.`x' if year > L.year
g ln`x'=ln(`x')
g lln`x'=L.ln`x'
g ln`x'c=ln`x'-lln`x'
}
label var lh_atotb "total wealth t-1"
label var lh_atotf "financial wealth t-1"


local var h_atotb h_atotf h_atotn atotf h_astck h_abond dtotb h_arles h_adebt h_atran  h_aothr h_acd h_absns h_ahoub h_achck h_aira cams_ctots cams_cmort cams_ctranss cams_cndur cams_cdurs cams_cautoall cams_chouss chtr_amount ch_amount oopmd hilt moneya diffm  transfer  dentst drinkd drinkn smoken cholst flusht breast mammog papsm  prost  henum lifein h_ins  liv10 


* wealth, consumption and other outcomes changes

foreach y of varlist `var' `var2' { 
gen `y'c= `y'-L.`y'
}

*Create house wealth level, first difference and log
	gen housw=h_atotb-h_atotn
	gen houswc=h_atotbc-h_atotnc
	gen lhousw=lh_atotb-lh_atotn
	

************************************************************************************************************************
************************************************************************************************************************
 
*******************************************************************************
** Other variables mainly for robustness checks***************************************
*******************************************************************************
 
*Create differences wrt market averages used for robustness checks

g diff_stck=h_astck-evf_stck
g diff_cd=h_acd-evf_cd
g diff_bond=h_abond-evf_bond
g diff_debt=h_adebt-evf_debt
g diff_chck =h_achck- evf_chck
g diff_totf = h_atotf-evf_totf
gen diff_fa=diff_stck+diff_cd+diff_bond-diff_debt
gen exp_diff= evf_totf-lh_atotf

*generate financial and risky portfolio variables
gen fa=h_astck+h_abond+h_acd-h_adebt
gen lfa=lh_astck+lh_abond+lh_acd-lh_adebt
gen ratio_fa= diff_fa/lfa
gen ratio_totf= diff_totf/lh_atotf

gen fa2=h_astck+h_abond+h_acd
gen lfa2=lh_astck+lh_abond+lh_acd
gen ratio_stock= diff_stck/lh_astck
gen ratio_bond= diff_bond/lh_abond
gen ratio_cd= diff_cd/lh_acd

gen risky_a=h_astck>0 if h_astck!=.
gen risky_s=h_astck/(h_astck+h_abond+h_acd+h_aothr+h_achck)
replace risky_s=0 if risky_s==. & risky_a==0
gen risky2_s=(h_astck+h_abond)/(h_astck+h_abond+h_acd+h_aothr+h_achck)
replace risky2_s=0 if risky_s==. & risky_a==0

gen safe_a=(h_abond+h_acd+h_achck)>0 if h_abond!=.
gen safe_s=(h_abond+h_acd+h_achck)/(atotf) if h_abond!=. & atotf>0

foreach y of varlist  risky_a risky_s safe_a safe_s {
gen `y'c=D.`y'
gen l`y'=L.`y'
}

g financial_activity = 0 if stck_buy == 5 & stck_sell_only == 5
replace financial_activity = 1 if stck_buy == 1 & financial_activity == .
replace financial_activity =1 if stck_sell_only == 1 & financial_activity == .
lab var financial_activity "financial activity -- self-reported"


*outcomes for other decisions in the Appendix
foreach y of varlist cholst flusht breast mammog papsm  prost {
replace `y'c=`y'-l2.`y' if `y'c==.
*these checks are not asked in consecutive waves
}
gen lh_ins=l.h_ins


  
	

*Wrong reporting of assests from asset verifications

*Missreporting of total wealth
egen wrong=rowtotal(wrongrecord_bond wrongrecord_bsns wrongrecord_cod wrongrecord_debts wrongrecord_eqln_prres wrongrecord_ira wrongrecord_mob_home wrongrecord_mrtg_prres wrongrecord_oth_asst wrongrecord_prim_res wrongrecord_real_est wrongrecord_trusts wrongrecord_stck wrongrecord_transport wrongrecord_svng_chck_acc wrongrecord_mrtg2_prres wrongrecord_scnd_home wrongrecord_mrtg_scnd_home)

*Missreporting of financial wealth
egen wrong_fa=rowtotal(wrongrecord_bond wrongrecord_bsns wrongrecord_cod wrongrecord_debts  wrongrecord_ira   wrongrecord_oth_asst  wrongrecord_trusts wrongrecord_stck  wrongrecord_svng_chck_acc)


*to check
local list h_abond h_absns h_acd h_achck h_adebt h_ahmln h_aira h_aothr h_arles h_astck h_atran

foreach y of local list{
	gen no_`y'=f_`y'==6
	gen i_`y'=1-(f_`y'<3 | f_`y'==6)
	replace no_`y'=. if f_`y'==.
	replace i_`y'=. if f_`y'==.
}



qui reg h_atotbc stablemem c.age##c.age educy i.raracem  t  ad_impw_atotf i.mstat  i.lbrf ragender  finr  i.cendiv if  ad_impw_atotb<.2, r
gen SB0=1 if e(sample)==1

compress
save HRS_pre_proxy, replace

*dropoing obseveration when recall or self-reported memory are missing
drop if tot_recall==.
drop if fpstme==1
drop if fimrc==1| fdlrc==1



*trimming bottom and top (half) percentiles. Different thresholds  for robustness checks.
	_pctile h_atotb, p(99.0)
	local topcode=r(r1)
	_pctile h_atotb, p(01.00)
	local bottomcode=r(r1)
	gen trim990=h_atotb<`topcode'&h_atotb>`bottomcode'
	

	
	_pctile h_atotb, p(99.5)
	local topcode=r(r1)
	_pctile h_atotb, p(00.5)
	local bottomcode=r(r1)
	gen trim995=h_atotb<`topcode'&h_atotb>`bottomcode'


_pctile h_atotb, p(99.75)
	local topcode=r(r1)
	_pctile h_atotb, p(00.25)
	local bottomcode=r(r1)
	gen trim975=h_atotb<`topcode'&h_atotb>`bottomcode'
	

	save HRS_before_trim, replace

	
*Define working samples

****************************************
************1. MAIN (STATIC) SAMPLE**************
****************************************

*final sample selection as described in the paper 
qui reg h_atotbc stablemem c.age##c.age educy i.raracem  t  ad_impw_atotf i.mstat  i.lbrf ragender  finr  i.cendiv lh_atotb lqrec
keep if trim990==1  & age<=20 & age>-10 &  ad_impw_atotb<.2 & e(sample)==1 // age 51+ at the first interview, imputation below 20% and no missings on the variables of interest


 
 xtset hhidpn t

 *generate previous wave wealth groups
 qui xtile q4bw= lh_atotb if lfinr==1, nq(4)
 qui xtile q4fw= lh_atotf if lfinr==1, nq(4)

save wsample1, replace

****************************************
*********2. EVENT STUDY SAMPLE**********
****************************************

use HRS_before_trim, clear

* Figure out which survey wave an individual is first observed in
bysort hhidpn (year): egen first_wave = min(year) 


*count number of losses
bysort hhidpn: egen tot_r_loss = total(r_loss)
bysort hhidpn: egen tot_r_loss_u = total(r_loss_u)
bysort hhidpn: egen tot_r_loss_a = total(r_loss_a)


*create first loss dummy, year and age
bysort hhidpn (t): egen first_r_loss = ifirst(r_loss) ,v(1) // 1 if first occurrence of r_loss

gen wave_loss = year if r_loss==1 
by hhidpn: egen first_loss_year = min(wave_loss)
gen time = (year - first_loss_year)/2

gen temp=age if first_r_loss==1
bysort hhidpn(t): egen age_loss=max(temp)
drop temp

*create a control group
gen sample_l= tot_r_loss>0 

*Define groups 

*control group placed at t-1
gen control=sample_l==0
replace time=-1 if control==1

*Aware and unaware based on the first episode
g temp=(first_r_loss == 1 & r_loss_u==1)
bys hhidpn: egen sample_u = max(temp)
drop temp
g temp=(first_r_loss == 1 & r_loss_a==1)
bys hhidpn: egen sample_a = max(temp)
drop temp

*classical DID in two versions
g post = time>= 0  

*gen interaction terms
foreach y of varlist  post  {
gen `y'_u=`y'*sample_u
gen `y'_a=`y'*sample_a
gen `y'_l=`y'*sample_l
}

*as in the main sample define financial respondent at t-1 based on time t=0
gen temp=lfinr==1 & time==0
bysort hhidpn: egen fr=max(temp)
drop temp

*create an indicator for switchers
bysort hhidpn: egen m_finr=mean(finr) 
gen fin_sw=m_finr>0 & finr<1



*remove missing to include it as loss cohort fixed effect 
 replace first_loss_year=0 if control==1



gen treatment=1 if sample_a==1
replace treatment=2 if sample_u==1

global X0 "age age2 i.t"
global X "$X0 ad_impw_atotb i.mstat  educy i.lbrf ragender i.raracem finr i.cendiv" 
 
 *Final sample of financial respondents in the age range with no missings 
qui reg h_atotbc post   $X    lh_atotb if ad_impw_atotb<.2     [pw=wtresp] , r cl(hhid)
keep if e(sample)==1&  trim995==1 & ( fr==1 | lfinr==1 & control==1) & (age_loss<=20 & age_loss>-10 | control==1 &age<=20 & age>-10)  
*Full sample used for figure 
save ws2_full, replace


keep if time>=-2 & time<=2 
	

table time treatment
save wsample2, replace
}
